/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.grnt.buddy;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.grnt.buddy.BuddiesTreeControler.BuddiesCtlException;
import org.grnt.common.CommonFunc;
import org.grnt.dbcommon.ControlBean;
import static org.grnt.dbcommon.DataControler.TransOpt;

final class AddBuddy implements ControlBean {

	final Long buddyId;
	final String alias;
	final Long parentId;
	final Long ownerId;
	final Long userId;
	final Integer isGroup;
	final String sqlStr = "INSERT INTO gt_buddies_tree" 
		+ "(buddy_id, alias, parent, owner, user_id, is_group)"
		+ "VALUES(?, '?', ?, ?, ?, ?)";

	/**
	 *
	 * Constructure
	 * @param buddyId An identify of buddy
	 * @param alias The buddy's name. Which name is made by a user.(Every buddy
	 * is belong to a correct user)
	 * @param parent All buddies of one user is organized as a tree. So there
	 * are two types buddy, one is personal buddy references to a correnct user,
	 * another is group buddy which is a branch in the tree so this style buddy
	 * include many children. By over reason, if a personal buddy must has a
	 * parent. A group buddy except root also has a parent buddy.
	 * @param owner The over has discussed every buddy must belong to a persion
	 * So the owner is the persion.
	 * @param userId When the buddy is persional buddy this file will has significative
	 * value. The value is the user it references to.
	 * @param isGroup Wheter it is a group user.
	 */
	public AddBuddy(Long buddyId, String alias, Long parent, Long owner, Long userId, Boolean isGroup) {
		this.buddyId = buddyId;
		this.alias = alias;
		this.parentId = parent;
		this.ownerId = owner;
		this.userId = userId;
		this.isGroup = isGroup ? 1 : 0;
	}

	/**
	 * This method provides DataControler to call the every of commonds in the return list.
	 * @return A serial ddl commands in a list.
	 */
	final public List<String> getCommands() {
		List<String> result = new ArrayList<String>();
		String command = CommonFunc.sqlStrFormat(sqlStr
			, buddyId == null ? null : buddyId.toString()
			, alias
			, parentId == null ? null : parentId.toString()
			, ownerId == null ? null : ownerId.toString()
			, userId == null ? null : userId.toString()
			, isGroup == null ? null : isGroup.toString());
		command = CommonFunc.sqlNullStr2NullVal(command);
		result.add(command);
		return result;
	}
	BuddyBean newBuddy = null;

	/**
	 *
	 * In this method, one BuddyBean object will be instanced. And then be added to
	 * context by calling function BuddiesTreeControler.addBuddy(newBuddy).
	 * @return If function BuddiesTreeControler.addBuddy(newBuddy) can
	 */
	final public boolean doInTransaction() {
		newBuddy = new BuddyBean();
		newBuddy.init(buddyId, alias, parentId, ownerId, userId, isGroup == 1);
		try {
			BuddiesTreeControler.addBuddy(newBuddy);
			return true;
		} catch (BuddiesCtlException ex) {
			Logger.getLogger(AddBuddy.class.getName()).log(Level.SEVERE, null, ex);
			return false;
		}
	}

	/**
	 * 
	 * If DataControler class sends a ROLLBACK value, newBuddy should be killed.
	 * @param action
	 */
	public void doAfterTransaction(TransOpt opt) {
		if (opt == TransOpt.ROLLBACK) {
			newBuddy.kill();
		}
	}
}
